package com.vhly.remoteapi.yahoo.contact.field;

import com.vhly.remoteapi.yahoo.contact.FieldValue;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-5
 * Time: 17:12:46
 * Email: vhly@163.com
 */
public class Address extends FieldValue {

    public String street;
    public String city;
    public String stateOrProvince;
    public String postalCode;
    public String country;
    public String countryCode;


    @Override
    public String toXML() {
        return null;
    }

    /**
     * read object from DataInput
     *
     * @param din DataInput
     */
    @Override
    public void readObject(DataInput din) throws IOException {
        byte b = din.readByte();
        if (b == 1) {
            street = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            city = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            stateOrProvince = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            postalCode = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            country = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            countryCode = din.readUTF();
        }
    }

    /**
     * Write object to DataOutput
     *
     * @param dout DataOutput
     */
    @Override
    public void writeObject(DataOutput dout) throws IOException {
        if (street != null) {
            dout.writeByte(1);
            dout.writeUTF(street);
        } else {
            dout.writeByte(0);
        }
        if (city != null) {
            dout.writeByte(1);
            dout.writeUTF(city);
        } else {
            dout.writeByte(0);
        }
        if (stateOrProvince != null) {
            dout.writeByte(1);
            dout.writeUTF(stateOrProvince);
        } else {
            dout.writeByte(0);
        }
        if (postalCode != null) {
            dout.writeByte(1);
            dout.writeUTF(postalCode);
        } else {
            dout.writeByte(0);
        }
        if (country != null) {
            dout.writeByte(1);
            dout.writeUTF(country);
        } else {
            dout.writeByte(0);
        }
        if (countryCode != null) {
            dout.writeByte(1);
            dout.writeUTF(countryCode);
        } else {
            dout.writeByte(0);
        }
    }

    /**
     * Parse Element
     *
     * @param el Element
     */
    @Override
    public void parse(Element el) {
        NodeList lst = el.getElementsByTagName("street");
        if (lst != null && lst.getLength() > 0) {
            Element eit = (Element) lst.item(0);
            NodeList nodes = eit.getChildNodes();
            if (nodes != null && nodes.getLength() > 0) {
                Text txt = (Text) nodes.item(0);
                street = txt.getNodeValue();
            }
        }

        lst = el.getElementsByTagName("city");
        if (lst != null && lst.getLength() > 0) {
            Element eit = (Element) lst.item(0);
            NodeList nodes = eit.getChildNodes();
            if (nodes != null && nodes.getLength() > 0) {
                Text txt = (Text) nodes.item(0);
                city = txt.getNodeValue();
            }
        }

        lst = el.getElementsByTagName("stateOrProvince");
        if (lst != null && lst.getLength() > 0) {
            Element eit = (Element) lst.item(0);
            NodeList nodes = eit.getChildNodes();
            if (nodes != null && nodes.getLength() > 0) {
                Text txt = (Text) nodes.item(0);
                stateOrProvince = txt.getNodeValue();
            }
        }

        lst = el.getElementsByTagName("postalCode");
        if (lst != null && lst.getLength() > 0) {
            Element eit = (Element) lst.item(0);
            NodeList nodes = eit.getChildNodes();
            if (nodes != null && nodes.getLength() > 0) {
                Text txt = (Text) nodes.item(0);
                postalCode = txt.getNodeValue();
            }
        }

        lst = el.getElementsByTagName("country");
        if (lst != null && lst.getLength() > 0) {
            Element eit = (Element) lst.item(0);
            NodeList nodes = eit.getChildNodes();
            if (nodes != null && nodes.getLength() > 0) {
                Text txt = (Text) nodes.item(0);
                country = txt.getNodeValue();
            }
        }

        lst = el.getElementsByTagName("countryCode");
        if (lst != null && lst.getLength() > 0) {
            Element eit = (Element) lst.item(0);
            NodeList nodes = eit.getChildNodes();
            if (nodes != null && nodes.getLength() > 0) {
                Text txt = (Text) nodes.item(0);
                countryCode = txt.getNodeValue();
            }
        }

    }
}
